
package cn.edu.scau.cmi.hombio.dao;

import cn.edu.scau.cmi.hombio.domain.Medicine;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.skyway.spring.util.dao.AbstractJpaDao;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

/**
 * DAO to manage Medicine entities.
 * 
 */
@Repository("MedicineDAO")

@Transactional
public class MedicineDAOImpl extends AbstractJpaDao<Medicine> implements MedicineDAO {

	/**
	 * Set of entity classes managed by this DAO.  Typically a DAO manages a single entity.
	 *
	 */
	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(Arrays.asList(new Class<?>[] {
			Medicine.class }));

	/**
	 * EntityManager injected by Spring for persistence unit pig
	 *
	 */
	@PersistenceContext(unitName = "pig")
	private EntityManager entityManager;

	/**
	 * Instantiates a new MedicineDAOImpl
	 *
	 */
	public MedicineDAOImpl() {
		super();
	}

	/**
	 * Get the entity manager that manages persistence unit 
	 *
	 */
	public EntityManager getEntityManager() {
		return entityManager;
	}

	/**
	 * Returns the set of entity classes managed by this DAO.
	 *
	 */
	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	/**
	 * JPQL Query - findMedicineByName
	 *
	 */
	@Transactional
	public Set<Medicine> findMedicineByName(String name) throws DataAccessException {

		return findMedicineByName(name, -1, -1);
	}

	/**
	 * JPQL Query - findMedicineByName
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Medicine> findMedicineByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findMedicineByName", startResult, maxRows, name);
		return new LinkedHashSet<Medicine>(query.getResultList());
	}

	/**
	 * JPQL Query - findMedicineByPhoneContaining
	 *
	 */
	@Transactional
	public Set<Medicine> findMedicineByPhoneContaining(String phone) throws DataAccessException {

		return findMedicineByPhoneContaining(phone, -1, -1);
	}

	/**
	 * JPQL Query - findMedicineByPhoneContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Medicine> findMedicineByPhoneContaining(String phone, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findMedicineByPhoneContaining", startResult, maxRows, phone);
		return new LinkedHashSet<Medicine>(query.getResultList());
	}

	/**
	 * JPQL Query - findMedicineByCompanyContaining
	 *
	 */
	@Transactional
	public Set<Medicine> findMedicineByCompanyContaining(String company) throws DataAccessException {

		return findMedicineByCompanyContaining(company, -1, -1);
	}

	/**
	 * JPQL Query - findMedicineByCompanyContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Medicine> findMedicineByCompanyContaining(String company, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findMedicineByCompanyContaining", startResult, maxRows, company);
		return new LinkedHashSet<Medicine>(query.getResultList());
	}

	/**
	 * JPQL Query - findMedicineByPhone
	 *
	 */
	@Transactional
	public Set<Medicine> findMedicineByPhone(String phone) throws DataAccessException {

		return findMedicineByPhone(phone, -1, -1);
	}

	/**
	 * JPQL Query - findMedicineByPhone
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Medicine> findMedicineByPhone(String phone, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findMedicineByPhone", startResult, maxRows, phone);
		return new LinkedHashSet<Medicine>(query.getResultList());
	}

	/**
	 * JPQL Query - findMedicineByNameContaining
	 *
	 */
	@Transactional
	public Set<Medicine> findMedicineByNameContaining(String name) throws DataAccessException {

		return findMedicineByNameContaining(name, -1, -1);
	}

	/**
	 * JPQL Query - findMedicineByNameContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Medicine> findMedicineByNameContaining(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findMedicineByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Medicine>(query.getResultList());
	}

	/**
	 * JPQL Query - findAllMedicines
	 *
	 */
	@Transactional
	public Set<Medicine> findAllMedicines() throws DataAccessException {

		return findAllMedicines(-1, -1);
	}

	/**
	 * JPQL Query - findAllMedicines
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Medicine> findAllMedicines(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllMedicines", startResult, maxRows);
		return new LinkedHashSet<Medicine>(query.getResultList());
	}

	/**
	 * JPQL Query - findMedicineByQuantityPerBox
	 *
	 */
	@Transactional
	public Set<Medicine> findMedicineByQuantityPerBox(Integer quantityPerBox) throws DataAccessException {

		return findMedicineByQuantityPerBox(quantityPerBox, -1, -1);
	}

	/**
	 * JPQL Query - findMedicineByQuantityPerBox
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Medicine> findMedicineByQuantityPerBox(Integer quantityPerBox, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findMedicineByQuantityPerBox", startResult, maxRows, quantityPerBox);
		return new LinkedHashSet<Medicine>(query.getResultList());
	}

	/**
	 * JPQL Query - findMedicineByQuantityPerCase
	 *
	 */
	@Transactional
	public Set<Medicine> findMedicineByQuantityPerCase(Integer quantityPerCase) throws DataAccessException {

		return findMedicineByQuantityPerCase(quantityPerCase, -1, -1);
	}

	/**
	 * JPQL Query - findMedicineByQuantityPerCase
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Medicine> findMedicineByQuantityPerCase(Integer quantityPerCase, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findMedicineByQuantityPerCase", startResult, maxRows, quantityPerCase);
		return new LinkedHashSet<Medicine>(query.getResultList());
	}

	/**
	 * JPQL Query - findMedicineByNumber
	 *
	 */
	@Transactional
	public Set<Medicine> findMedicineByNumber(String number) throws DataAccessException {

		return findMedicineByNumber(number, -1, -1);
	}

	/**
	 * JPQL Query - findMedicineByNumber
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Medicine> findMedicineByNumber(String number, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findMedicineByNumber", startResult, maxRows, number);
		return new LinkedHashSet<Medicine>(query.getResultList());
	}

	/**
	 * JPQL Query - findMedicineByPrimaryKey
	 *
	 */
	@Transactional
	public Medicine findMedicineByPrimaryKey(Integer id) throws DataAccessException {

		return findMedicineByPrimaryKey(id, -1, -1);
	}

	/**
	 * JPQL Query - findMedicineByPrimaryKey
	 *
	 */

	@Transactional
	public Medicine findMedicineByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findMedicineByPrimaryKey", startResult, maxRows, id);
			return (cn.edu.scau.cmi.hombio.domain.Medicine) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findMedicineByMemo
	 *
	 */
	@Transactional
	public Set<Medicine> findMedicineByMemo(String memo) throws DataAccessException {

		return findMedicineByMemo(memo, -1, -1);
	}

	/**
	 * JPQL Query - findMedicineByMemo
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Medicine> findMedicineByMemo(String memo, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findMedicineByMemo", startResult, maxRows, memo);
		return new LinkedHashSet<Medicine>(query.getResultList());
	}

	/**
	 * JPQL Query - findMedicineById
	 *
	 */
	@Transactional
	public Medicine findMedicineById(Integer id) throws DataAccessException {

		return findMedicineById(id, -1, -1);
	}

	/**
	 * JPQL Query - findMedicineById
	 *
	 */

	@Transactional
	public Medicine findMedicineById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findMedicineById", startResult, maxRows, id);
			return (cn.edu.scau.cmi.hombio.domain.Medicine) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findMedicineByNumberContaining
	 *
	 */
	@Transactional
	public Set<Medicine> findMedicineByNumberContaining(String number) throws DataAccessException {

		return findMedicineByNumberContaining(number, -1, -1);
	}

	/**
	 * JPQL Query - findMedicineByNumberContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Medicine> findMedicineByNumberContaining(String number, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findMedicineByNumberContaining", startResult, maxRows, number);
		return new LinkedHashSet<Medicine>(query.getResultList());
	}

	/**
	 * JPQL Query - findMedicineByMemoContaining
	 *
	 */
	@Transactional
	public Set<Medicine> findMedicineByMemoContaining(String memo) throws DataAccessException {

		return findMedicineByMemoContaining(memo, -1, -1);
	}

	/**
	 * JPQL Query - findMedicineByMemoContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Medicine> findMedicineByMemoContaining(String memo, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findMedicineByMemoContaining", startResult, maxRows, memo);
		return new LinkedHashSet<Medicine>(query.getResultList());
	}

	/**
	 * JPQL Query - findMedicineByCompany
	 *
	 */
	@Transactional
	public Set<Medicine> findMedicineByCompany(String company) throws DataAccessException {

		return findMedicineByCompany(company, -1, -1);
	}

	/**
	 * JPQL Query - findMedicineByCompany
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Medicine> findMedicineByCompany(String company, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findMedicineByCompany", startResult, maxRows, company);
		return new LinkedHashSet<Medicine>(query.getResultList());
	}

	/**
	 * Used to determine whether or not to merge the entity or persist the entity when calling Store
	 * @see store
	 * 
	 *
	 */
	public boolean canBeMerged(Medicine entity) {
		return true;
	}
}
